﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net.Mail;
using System.Data;
using Workflow.Alerts.DAL;
using workflow.Dal;
namespace Workflow.Alerts.Utils
{
    public class Notification
    {

        private const string IP = "localhost";
        struct MailParts
        {
            public string toName;
            public string toMail;
            public string fromName;
            public string fromOrganization;
            public string fromPhone;
            public string fromMail;
            public string statusName;
            public string url;
        }

        public static void SendNotification(string alertId, int status, EstateDataSet.ContactAlertsRow contacts)
        {
            MailParts mailParts = GetMailPartsByStatus((WorkflowEnums.Status)status, contacts);

            string link = string.Format("http://workflow.estate.nl/{0}/detailpage.aspx?alertId={1}", mailParts.url, alertId);

            mailParts.statusName = Translate.Words.Keys.Contains(mailParts.statusName) ? Translate.Words[mailParts.statusName] : mailParts.statusName;
            string subject = string.Format("Melding {0} is {1}", contacts.code, mailParts.statusName);

            MailMessage msg = new MailMessage();
            msg.Subject = subject;
            msg.Body = CreateMessageBody(mailParts, subject, link);
            msg.To.Add(new MailAddress(mailParts.toMail));
            msg.From = new MailAddress("workflow@estate.nl");
            SendEmail(msg);
        }

        private static MailParts GetMailPartsByStatus(WorkflowEnums.Status status, EstateDataSet.ContactAlertsRow contacts)
        {
            MailParts mailParts = new MailParts();
            switch (status)
            {
                case WorkflowEnums.Status.Approved:
                case WorkflowEnums.Status.PartiallyApproved:
                case WorkflowEnums.Status.Declined:
                    mailParts.url = "NetworkOperator/Alerts/InProgress";
                    mailParts.statusName = Enum.GetName(typeof(WorkflowEnums.Status), status);
                    mailParts.toName = contacts.noname;
                    mailParts.toMail = contacts.noemail;
                    mailParts.fromName = contacts.munname;
                    mailParts.fromOrganization = string.Format("Municipality {0}", contacts.municipalityname);
                    mailParts.fromPhone = contacts.munphone;
                    mailParts.fromMail = contacts.munemail;
                    break;
                case WorkflowEnums.Status.Rejected:
                    mailParts.url = "NetworkOperator/Alerts/Logged";
                    mailParts.statusName = "Rejected";
                    mailParts.toName = contacts.noname;
                    mailParts.toMail = contacts.noemail;
                    mailParts.fromName = contacts.munname;
                    mailParts.fromOrganization = string.Format("Municipality {0}", contacts.municipalityname);
                    mailParts.fromPhone = contacts.munphone;
                    mailParts.fromMail = contacts.munemail;
                    break;
                case WorkflowEnums.Status.Executed4Municipality:
                case WorkflowEnums.Status.Executed4Contractor:
                    mailParts.url = string.Concat(contacts.municipalityname, "/Alerts/Executed");
                    mailParts.statusName = "Executed";
                    mailParts.toName = contacts.munname;
                    mailParts.toMail = contacts.munemail;
                    mailParts.fromName = contacts.noname;
                    mailParts.fromOrganization = contacts.networkoperatorname;
                    mailParts.fromPhone = contacts.nophone;
                    mailParts.fromMail = contacts.noemail;
                    break;
                case WorkflowEnums.Status.Accepted:
                    mailParts.url = "NetworkOperator/Alerts/Executed";
                    mailParts.statusName = "Accepted";
                    mailParts.toName = contacts.noname;
                    mailParts.toMail = contacts.noemail;
                    mailParts.fromName = contacts.munname;
                    mailParts.fromOrganization = string.Format("Municipality {0}", contacts.municipalityname);
                    mailParts.fromPhone = contacts.munphone;
                    mailParts.fromMail = contacts.munemail;
                    break;
                case WorkflowEnums.Status.AcceptedPartially:
                    mailParts.url = "NetworkOperator/Alerts/Executed";
                    mailParts.statusName = "Accepted With Exceptions";
                    mailParts.toName = contacts.noname;
                    mailParts.toMail = contacts.noemail;
                    mailParts.fromName = contacts.munname;
                    mailParts.fromOrganization = string.Format("Municipality {0}", contacts.municipalityname);
                    mailParts.fromPhone = contacts.munphone;
                    mailParts.fromMail = contacts.munemail;
                    break;
                default:
                    break;
            }
            return mailParts;
        }

        private static string CreateMessageBody(MailParts mailParts, string subject, string link)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("Beste {0},", mailParts.toName));
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine("");
            sb.AppendLine(subject);
            sb.AppendLine("Klik hieronder om naar uw melding te gaan.");
            sb.AppendLine("");
            sb.AppendLine(link);
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine("Met vriendelijke groet,");
            sb.AppendLine(mailParts.fromName);
            sb.AppendLine(mailParts.fromOrganization);
            sb.AppendLine(mailParts.fromPhone);
            sb.AppendLine(mailParts.fromMail);
            sb.AppendLine(Environment.NewLine);

            return sb.ToString();
        }

        private static string CreateActivityTypeMessageBody(Alert alert, string oldActivityType)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("Beste {0},", alert.networkoperatorname));
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine("");
            sb.AppendLine(string.Format("Status for alert {0} has been chaged from {1} to {2}", alert.code, oldActivityType, WorkflowEnums.ActivityTypeList[alert.activitytype]));


            sb.AppendLine(Environment.NewLine);
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine("Met vriendelijke groet,");
            if (alert.Municipality == null)
                alert.MunicipalityReference.Load();
            alert.Municipality.PersonReference.Load();
            sb.AppendLine(string.Format("{0} {1}", alert.Municipality.Person.firstname, alert.Municipality.Person.lastname));
            sb.AppendLine(alert.municipalityname);
            sb.AppendLine(alert.Municipality.Person.Phone1);
            sb.AppendLine(alert.Municipality.Person.email);
            sb.AppendLine(Environment.NewLine);

            return sb.ToString();
        }

        private static string CreateTrafficCommentsChangedMessageBody(Alert alert, string comments, User currentUser)
        {
            StringBuilder sb = new StringBuilder();
            if (currentUser.Type == WorkflowEnums.UserType.Municipality)
            {
                sb.AppendLine(string.Format("Beste {0},", alert.networkoperatorname));
            }
            else
            {
                sb.AppendLine(string.Format("Beste {0},", alert.municipalityname));
            }
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine("");
            sb.AppendLine("There were changes in traffic related comments:");
            sb.AppendLine(string.Format("{0} {1} commented:", currentUser.FirstName, currentUser.LastName));
            sb.AppendLine(comments);

            sb.AppendLine(Environment.NewLine);
            sb.AppendLine(Environment.NewLine);
            sb.AppendLine("Met vriendelijke groet,");


            if (currentUser.Type == WorkflowEnums.UserType.Municipality)
            {
                if (alert.Municipality == null)
                {
                    alert.MunicipalityReference.Load();
                    alert.Municipality.PersonReference.Load();
                }
                sb.AppendLine(string.Format("{0} {1}", alert.Municipality.Person.firstname, alert.Municipality.Person.lastname));
                sb.AppendLine(alert.municipalityname);
                sb.AppendLine(alert.Municipality.Person.Phone1);
                sb.AppendLine(alert.Municipality.Person.email);
            }
            else
            {
                if (alert.ContactNetworkUser == null)
                    alert.ContactNetworkUserReference.Load();

                sb.AppendLine(string.Format("{0} {1}", alert.ContactNetworkUser.firstname, alert.ContactNetworkUser.lastname));
                sb.AppendLine(alert.networkoperatorname);
                sb.AppendLine(alert.ContactNetworkUser.Phone1);
                sb.AppendLine(alert.ContactNetworkUser.email);
            }
            sb.AppendLine(Environment.NewLine);

            return sb.ToString();
        }

        public static void SendActivityChangedNotification(Alert alert, string oldActivityType)
        {

            MailMessage msg = new MailMessage();
            msg.Subject = "Change of activity type";
            msg.Body = CreateActivityTypeMessageBody(alert, oldActivityType);
            if (alert.ContactNetworkUser == null)
                alert.ContactNetworkUserReference.Load();
            msg.To.Add(new MailAddress(alert.ContactNetworkUser.email));
            msg.From = new MailAddress("workflow@estate.nl");

            SendEmail(msg);
        }

        public static void SendTrafficCommentsChangedNotification(Alert alert, string comments, User currentUser)
        {
            MailMessage msg = new MailMessage();
            msg.Subject = "Traffic status has been updated";
            msg.Body = CreateTrafficCommentsChangedMessageBody(alert, comments, currentUser);

            if (currentUser.Type == WorkflowEnums.UserType.Municipality)
            {
                if (alert.ContactNetworkUser == null)
                    alert.ContactNetworkUserReference.Load();
                msg.To.Add(new MailAddress(alert.ContactNetworkUser.email));
            }
            else
            {
                if (alert.Municipality == null)
                {
                    alert.MunicipalityReference.Load();
                    alert.Municipality.PersonReference.Load();
                }
                msg.To.Add(new MailAddress(alert.Municipality.Person.email));
            }

            msg.From = new MailAddress("workflow@estate.nl");
            SendEmail(msg);
        }

        private static void SendEmail(MailMessage msg)
        {
            try
            {
                SmtpClient client = new SmtpClient(IP);
                client.Send(msg);
            }
            catch
            {
            }
        }
    }
}
